/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Assignment.BLO;

import Assignment.DAO.UserDAO;
import Assignment.DTO.UserDTO;
import Assignment.DTO.UserListDTO;
import Assignment.Util.ConnectionUtil;
import com.sun.xml.stream.events.XMLEventAllocatorImpl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author dieptn
 */
public class UserBLO {

    private Connection con;

    public String getAllUsers() {
        String path = "";
        try {
            con = ConnectionUtil.getConnection();
            JAXBContext ctx = JAXBContext.newInstance(UserListDTO.class);
            Marshaller marshal = ctx.createMarshaller();
            marshal.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            ArrayList<UserDTO> list = new ArrayList<UserDTO>();
            list = UserDAO.search(con, null);
            UserListDTO doc = new UserListDTO();
            for (int i = 0; i < list.size(); i++) {
                doc.getUserList().add(list.get(i));
            }
            //path = "C:\\users.xml";
            path = ConnectionUtil.getUserFilePath();
            marshal.marshal(doc, new File(path));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    public boolean registerUser(UserDTO user) {
        con = ConnectionUtil.getConnection();
        if (UserDAO.insert(con, user) > 0) {
            return true;
        }
        return false;
    }

    public boolean updateUser(UserDTO user) {
        con = ConnectionUtil.getConnection();
        if (UserDAO.update(con, user) > 0) {
            return true;
        }
        return false;
    }
}
